from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
from time import sleep
import os
import datetime
import json


def write_bilibili_video_data(item):
    source_dir = 'video_data'
    target_dir = os.path.join(source_dir, item['videoid'])
    if not os.path.exists(target_dir):
        os.mkdir(target_dir)
    file_name = "{}.txt".format(datetime.datetime.now().strftime("%Y%m%d"))
    file_path = os.path.join(target_dir, file_name)
    with open(file_path, mode='a', encoding='utf8') as file:  # r w rb wb a
        file.write(json.dumps(item, ensure_ascii=False))
        file.write('\n')


def parse_video_url_info(driver, videoid):
    url = 'https://www.bilibili.com/video/{}/'.format(videoid)
    driver.get(url)
    sleep(1)

    item = {}
    item['title'] = driver.find_element(By.XPATH, './/h1[@class="video-title"]').get_attribute('title')
    item['link'] = url
    item['videoid'] = videoid
    item['play_time'] = driver.find_element(By.XPATH, './/div[@class="video-info-detail-list"]/span[1]').text
    item['comments'] = driver.find_element(By.XPATH, './/div[@class="video-info-detail-list"]/span[2]').text
    item['duration'] = driver.find_element(By.XPATH, './/span[@class="bpx-player-ctrl-time-duration"]').text
    item['author'] = driver.find_element(By.XPATH, './/div[@class="up-detail-top"]/a[1]').text
    item['date'] = driver.find_element(By.XPATH, './/span[@class="pubdate-text"]').text
    item['datetime'] = datetime.datetime.now().strftime("%Y%m%d%H%M%S")

    print(item)
    sleep(1)
    return item


def read_bilibili_video_data(driver):
    source_dir = 'video_data'
    for dir in os.listdir(source_dir):
        try:
            item = parse_video_url_info(driver, dir)
            write_bilibili_video_data(item)
        except:
            continue


if __name__ == "__main__":
    chrome_options = Options()
    chrome_options.add_argument('--headless')
    chrome_options.add_argument("--window-size=3840,2160")
    prefs = {'profile.managed_default_content_settings.images': 2}
    chrome_options.add_experimental_option('prefs', prefs)
    driver = webdriver.Chrome(chrome_options=chrome_options)

    read_bilibili_video_data(driver)

    driver.quit()

